package sun.scheduler.core.job;

/**
 * A class used for the rounding process of the resources of a given {@link JobInfo}.
 *
 * @author mfj
 */
public class RoundingInfo extends Process
{

    /**
     * Gets the state of the calculation.
     */
    public final static Job.CurveState STATE = Job.CurveState.Rounding;
    /**
     * The roundet whole number resources spend by the job.
     */
    public int roundetResources;
    /**
     * The roundet resources spend by the job.
     */
    public long duration;
    /**
     * The job which this flow computation belongs to.
     */
    public final Job baseJob;

    /**
     * Instantiates a new instance of the {@link RoundingInfo} from a {@link LTCTInfo}
     * instance after the LTCT process is completed.
     *
     * The ressource are ceiled to either the next bigger whole number or the
     * maximum amount of resources given by the basejob if ceiling would
     * comromise the upper ressource bound.
     *
     * @param info the {@link LTCTInfo} instance of the job
     */
    public RoundingInfo(LTCTInfo info)
    {
        this.baseJob = info.baseJob;
        round();
    }
        
    @Override
    public long getDuration()
    {
        return duration;
    }
    
    @Override
    public double getAmountOfResources()
    {
        return roundetResources;
    }
    
    public final void round()
    {
        LTCTInfo info = (LTCTInfo) baseJob.getProcess(Job.CurveState.LTCT);
        
        double rResources = Math.round(info.getAmountOfResources());
        if (baseJob.maxResources < rResources)
            rResources = baseJob.maxResources;
        else if (baseJob.minResources > rResources)
            rResources = baseJob.minResources;
       
        this.roundetResources = (int) rResources;
        this.duration = (long) baseJob.durationOfResources(roundetResources);        
    }
    
}
